package org.thingsboard.server.dao.model.sql.custom;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.TypeDef;
import org.thingsboard.server.common.data.custom.DeviceGroup;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.custom.DeviceGroupId;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.util.mapping.JsonStringType;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.UUID;


@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@Table(name = "c_tb_device_group")
public class DeviceGroupEntity extends BaseSqlEntity<DeviceGroup>{

    @Column(name = "name")
    private String name;

    //父节点id
    @Column(name = "parent_id", columnDefinition = "uuid")
    private UUID parentId;
    //节点层级
    @Column(name = "level")
    private Integer level;

    //分类标识
    @Column(name = "key")
    private String key;

    //子分组排序
    @Column(name = "sort_index")
    private Integer sortIndex;

    @Column(name = "tenant_id", columnDefinition = "uuid")
    private UUID tenantId;

    @Column(name = "customer_id", columnDefinition = "uuid")
    private UUID customerId;
    //分组状态
    @Column(name = "status")
    private Boolean status;


    @Override
    public DeviceGroup toData() {
        DeviceGroup deviceGroup = new DeviceGroup(new DeviceGroupId(id));

        if (tenantId != null) {
            deviceGroup.setTenantId(TenantId.fromUUID(tenantId));
        }
        if (customerId != null) {
            deviceGroup.setCustomerId(new CustomerId(customerId));
        }

        deviceGroup.setName(name);
        deviceGroup.setLevel(level);
        deviceGroup.setParentId(new DeviceGroupId(parentId));
        deviceGroup.setKey(key);
        deviceGroup.setSortIndex(sortIndex);
        deviceGroup.setCreatedTime(createdTime);
        deviceGroup.setStatus(status);

        return deviceGroup;
    }
}
